class AccountRecord < ActiveRecord::Base
  belongs_to:account
  belongs_to:order
  
  @@opp={'income'=>"收入",'expense'=>"支出"}
  
  def self.opp
    return @@opp
  end
  
  def buyer_paid(account,order)
    self.order=order
    self.account=account
    self.operation="order_paid"
    self.operation_prop="expense"
    self.money_amount=order.order_price_sum
    self.money_left=account.account_money
  end
  
  def add_money(params)
    params[:operation]="存入"
    income(params)
  end
  
  def user_paid(params)
    params[:operation]="支付订单"
    expense(params)
  end
  
  def admin_income(params)
    params[:operation]="支付订单"
    income(params)
  end
  
private
  def income(params)
    params[:prop]='income'
    record(params)
  end
  
  def expense(params)
    params[:prop]='expense'
    record(params)
  end
  
  def record(params)
    if params[:account]&&params[:money]&&params[:operation]&&params[:prop]||params[:order]
      account=params[:account]
      money=params[:money]
      operation=params[:operation]
      prop=params[:prop]
      order=params[:order]
            
      self.order=order
      self.account=account
      self.operation_prop=prop
      self.operation=operation
      self.money_amount=money
      self.money_left=account.account_money
      return true
    else
      return false
    end

  end
  
end
